package com.example.qimo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.qimo.dao.Permission;
import com.example.qimo.dao.Role;
import com.example.qimo.dao.User;
import com.example.qimo.mapper.PermissionMapper;
import com.example.qimo.mapper.RoleMapper;
import com.example.qimo.mapper.UserMapper;
import jakarta.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.concurrent.CompletableFuture;
    @Service
    public class UserService extends ServiceImpl<UserMapper, User> implements IService<User> {
        @Resource
        private RoleMapper roleMapper;
        @Resource
        private PermissionMapper permissionMapper;
        @Resource
        private UserMapper userMapper;

        @Cacheable(value = "user", key = "'user:' +#name")
        public User getByName(String name){
            LambdaQueryWrapper<User> qr = new LambdaQueryWrapper<>();
            qr.eq(User::getUsername,name);
            User user = getOne(qr);
            return user ;
        }

        public List<Role> findRoleByUserRole(String roleName){
            return roleMapper.selectList(new LambdaQueryWrapper<Role>()
                    .eq(Role::getRoleName,roleName));
        }
        public List<Permission> findPermissionByRoleId(int roleId){
            return permissionMapper.permissionList(roleId);
        }

        //根据用户名查找记录
//    public User getByName(String name){
//        LambdaQueryWrapper<User> qr = new LambdaQueryWrapper<>();
//        qr.eq(User::getUsername,name);
//        return userMapper.selectOne(qr);
//    }
        @Async("taskExecutor")
        public CompletableFuture<String> asyncMethod(){
//            Thread.sleep(2000); // 模拟长时间的任务
//            System.out.println("子线程的线程名称"+Thread.currentThread().getName());
//            return CompletableFuture.completedFuture("任务完成");  //返回一个
            return CompletableFuture.supplyAsync(() -> { //supplyAsync带返回值    runAsync不带返回值
                if (Math.random() > 0.5) {
                    throw new RuntimeException("模拟异常");
                }
                return "任务完成";
            }).exceptionally(ex -> {
                // 异常处理
                return "任务失败：" + ex.getMessage();
            });
        }
        public List<User> saveUserCx(String name) {
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getUsername, name);
            return userMapper.selectList(lambdaQueryWrapper);
        }
        public IPage<User> getStudentList(int p) {
            IPage<User> page = new Page<>(p, 3);
            LambdaQueryWrapper<User> qr = new LambdaQueryWrapper<>();
//        return studentMapper.getUList(page,qr);
            return page(page,qr);
        }
    }
